package com.xqledu.admin.config;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author 向其林
 * @version 1.0
 * @description: TODO
 * @date 2023/5/17 0017 21:34
 */
@Configurable
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable() //禁用跨站csrf攻击防御
                .formLogin()
                .loginPage("/login")
                .and()
                .authorizeRequests() //配置访问权限
//                .antMatchers("/login.html").permitAll() //放行这个网页
//                .antMatchers("/biz1").hasAnyAuthority("biz1")  //前面是资源的访问路径、后面是资源的名称或者叫资源ID
//                .antMatchers("/biz2").hasAnyAuthority("biz2")
//                .antMatchers("/syslog").hasAnyAuthority("syslog")
//                .antMatchers("/sysuser").hasAnyAuthority("sysuser")
                .anyRequest().authenticated() //所有请求都需要认证
                .and()
                .httpBasic(); //配置基本属性
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user")
                .password("{noop}123456")//密码前面的{noop}表示的是指定的PasswordEncoder
                .authorities("biz1","biz2");//配置BCrypt加密
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //放行动态资源
        web.ignoring().antMatchers(
                "/*.html"
        );
    }
}
